﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ControleRepresentante.Logic.ImpostoHandlers;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using ControleRepresentante.Logic.Statistics;
using WebSite.Controllers.ControllerExtensions;
using WebSite.ViewModels;

namespace WebSite.Controllers
{
    public class CompareController : ControllerBase<IPedidoRepository>
    {
        public CompareController(IRepositoryProvider repositoryProvider, IControllerExtensionProvider controllerExtensionProvider, IImpostoProvider impostoProvider)
            : base(repositoryProvider, controllerExtensionProvider, impostoProvider)
        {
        }

        protected override Area? Area { get { return ControleRepresentante.Logic.Models.Area.Pedidos; } }

        public ActionResult Index(CompareTotal compareTotal = CompareTotal.TotalMercadoria, string fabricaIds = "", string vendedorIds = "", string cliente = "", string transportadoraIds = "", bool allJolitex = false)
        {
            EnsureHasPermission(Operacao.Compare);
            Dictionary<Guid, string> vendedores = ParseIds<IVendedorRepository, Vendedor, StatisticsBase>(vendedorIds, x => x.Nome);
            Dictionary<Guid, string> fabricas = ParseIds<IFabricaRepository, Fabrica, StatisticsBase>(fabricaIds, x => x.Nome);
            Dictionary<Guid, string> transportadoras = ParseIds<ITransportadoraRepository, Transportadora, StatisticsBase>(transportadoraIds, x => x.Nome);
            IEnumerable<CompareYear> compareYears = Repository.GetCompare(compareTotal, fabricas.Keys, vendedores.Keys, cliente, transportadoras.Keys, allJolitex);
            CompareViewModel compareViewModel = new CompareViewModel(compareYears)
            {
                AllJolitex = allJolitex,
                ClienteValue = cliente,
                CompareTotal = compareTotal,
                FabricaValue = fabricaIds,
                Fabricas = fabricas,
                VendedorValue = vendedorIds,
                Vendedores = vendedores,
                TransportadoraValue = transportadoraIds,
                Transportadoras = transportadoras
            };
            return View(compareViewModel);
        }
    }
}
